//
//  KKPersonalHomeVC.m
//  DoMi
//
//  Created by 康凯 on 2016/11/23.
//  Copyright © 2016年 kk. All rights reserved.
//

#import "KKPersonalHomeVC.h"
#import "KKIntroVC.h"
#import "KKPersonalVideoVC.h"
#import "KKPersonalArticleVC.h"
#import "KKAdviceChatVC.h"
#import "KKComHttp.h"
#import "KKExpertDetailM.h"
#import "KKShareDialog.h"
#import "KKFollowVC.h"
#import "KKMyHttp.h"

@interface KKPersonalHomeVC ()
@property (weak, nonatomic) IBOutlet UIView *container;
@property (weak, nonatomic) IBOutlet UIButton *intro;
@property (weak, nonatomic) IBOutlet UIButton *video;
@property (weak, nonatomic) IBOutlet UIButton *article;

@property (weak, nonatomic) IBOutlet UILabel *guanZhuCount;
@property (weak, nonatomic) IBOutlet UILabel *fanCount;
@property (weak, nonatomic) IBOutlet UILabel *commentCount;
@property (weak, nonatomic) IBOutlet UIImageView *avatarBg;
@property (weak, nonatomic) IBOutlet UIImageView *avatar;
@property (weak, nonatomic) IBOutlet UILabel *realName;
@property (weak, nonatomic) IBOutlet UILabel *company;
@property (weak, nonatomic) IBOutlet UILabel *job;
@property (weak, nonatomic) IBOutlet UIButton *consultBtn;
@property (weak, nonatomic) IBOutlet UIView *followView;
@property (weak, nonatomic) IBOutlet UIView *fanView;
@property (weak, nonatomic) IBOutlet UIView *commentView;
@property (weak, nonatomic) IBOutlet UIButton *gZBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *jobWidth;

@property (weak, nonatomic) KKIntroVC *introVC;
@property (weak, nonatomic) KKPersonalVideoVC *videoVC;
@property (weak, nonatomic) KKPersonalArticleVC *articleVC;

@property (weak, nonatomic) UIViewController *currentVC;
@property (strong,nonatomic) KKExpertDetailM *expertInfo;

@end

@implementation KKPersonalHomeVC
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setNavWhite];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    if(self.flag!=1){
        self.flag=0;
    }else{
        
    }
    self.view.backgroundColor=[UIColor colorWithHexString:@"#FEFFFF"];
    //初始化nav
    [self initNav];
    [self initView];
}
#pragma mark ---初始化nav
-(void)initNav{
    self.navigationItem.title=@"个人主页";
    //左侧排行榜
    UIButton *leftBtn = [[UIButton alloc ]initWithFrame:CGRectMake(0, 0, 12, 20)];
    [leftBtn setImage:[UIImage imageNamed:@"back_blue"] forState:UIControlStateNormal];
    
    [leftBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    
    //右侧分享
    UIButton *rightBtn = [[UIButton alloc ]initWithFrame:CGRectMake(0, 0, 20, 20)];
    [rightBtn setImage:[UIImage imageNamed:@"sharemini"] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(share) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
}

-(void)initData{
    self.guanZhuCount.text=self.expertInfo.gzcou;
    self.fanCount.text=self.expertInfo.fscou;
    self.commentCount.text=[NSString stringWithFormat:@"%@%@",self.expertInfo.judgerank,@"%"];
    
    [self.avatarBg sd_setImageWithURL:[NSURL URLWithString:self.expertInfo.backgroundimg] placeholderImage:[UIImage imageNamed:@"default_avater"]];
    [self.avatar sd_setImageWithURL:[NSURL URLWithString:self.expertInfo.avatar] placeholderImage:[UIImage imageNamed:@"default_avater"]];
    if([KKAppUtils isEmptyString:self.expertInfo.nickname]){
        self.realName.text=@"匿名";
    }else{
        self.realName.text=self.expertInfo.nickname;
    }
    
    self.company.text=self.expertInfo.company;
    float width=[KKAppUtils sizeWithText:self.expertInfo.job font:[UIFont systemFontOfSize:12] maxSize:CGSizeMake(100, 14)].width;
    self.jobWidth.constant=width+5;
    self.job.text=self.expertInfo.job;
    
    if([self.expertInfo.expertflag isEqualToString:@"0"]){
        self.consultBtn.hidden=YES;
    }
    if([self.expertInfo.gzflag isEqualToString:@"0"]){
        self.gZBtn.selected=NO;
    }else{
        self.gZBtn.selected=YES;
    }
}

#pragma mark ---初始化view
-(void)initView{
    //关注粉丝按钮
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(followBtn)];
    [self.followView addGestureRecognizer:tap];
    UITapGestureRecognizer *tap1=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(fanBtn)];
    [self.fanView addGestureRecognizer:tap1];
    
    
    KKIntroVC *introVC=[[KKIntroVC alloc]initWithNibName:@"KKIntroVC" bundle:nil];
    self.introVC=introVC;
    introVC.expertId=self.expertId;
    KKPersonalVideoVC *videoVC=[[KKPersonalVideoVC alloc]initWithNibName:@"KKPersonalVideoVC" bundle:nil];
    self.videoVC=videoVC;
    videoVC.expertId=self.expertId;
    KKPersonalArticleVC *articleVC=[[KKPersonalArticleVC alloc]initWithNibName:@"KKPersonalArticleVC" bundle:nil];
    self.articleVC=articleVC;
    articleVC.expertId=self.expertId;
    
    [self addChildViewController:introVC];
    [self addChildViewController:videoVC];
    [self addChildViewController:articleVC];
    
    self.currentVC=introVC;
    self.intro.selected=YES;
    //调整子视图控制器的Frame已适应容器View
    [self fitFrameForChildViewController:self.introVC];
    //设置默认显示在容器View的内容
    [self.container addSubview:self.introVC.view];
    
    [self getExpertDetail];
}

- (void)fitFrameForChildViewController:(UIViewController *)chileViewController{
    CGRect frame = self.container.frame;
    frame.origin.y = 0;
    chileViewController.view.frame = frame;
}

//转换子视图控制器
- (void)transitionFromOldViewController:(UIViewController *)oldViewController toNewViewController:(UIViewController *)newViewController{
    [self transitionFromViewController:oldViewController toViewController:newViewController duration:0.3 options:UIViewAnimationOptionTransitionCrossDissolve animations:nil completion:^(BOOL finished) {
        if (finished) {
            [newViewController didMoveToParentViewController:self];
            _currentVC = newViewController;
        }else{
            _currentVC = oldViewController;
        }
    }];
}



- (IBAction)introBtn:(id)sender {
    self.intro.selected=YES;
    self.video.selected=NO;
    self.article.selected=NO;
    if(self.currentVC==self.introVC){
        return;
    }
    [self fitFrameForChildViewController:self.introVC];
    [self transitionFromOldViewController:_currentVC toNewViewController:self.introVC];
}
- (IBAction)videoBtn:(id)sender {
    self.intro.selected=NO;
    self.video.selected=YES;
    self.article.selected=NO;
    if(self.currentVC==self.videoVC){
        return;
    }
    [self fitFrameForChildViewController:self.videoVC];
    [self transitionFromOldViewController:_currentVC toNewViewController:self.videoVC];
}
- (IBAction)articleBtn:(id)sender {
    self.intro.selected=NO;
    self.video.selected=NO;
    self.article.selected=YES;
    if(self.currentVC==self.articleVC){
        return;
    }
    [self fitFrameForChildViewController:self.articleVC];
    [self transitionFromOldViewController:_currentVC toNewViewController:self.articleVC];
}

#pragma mark 点击--返回
-(void)back{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark 按钮--关注
-(void)followBtn{
    KKFollowVC *follow=[[KKFollowVC alloc] init];
    follow.navTitle=@"关注";
    follow.userId=self.expertInfo.expertid;
    [self.navigationController pushViewController:follow animated:YES];
}
#pragma mark 按钮--粉丝
-(void)fanBtn{
    KKFollowVC *follow=[[KKFollowVC alloc] init];
    follow.navTitle=@"粉丝";
    follow.userId=self.expertInfo.expertid;
    [self.navigationController pushViewController:follow animated:YES];
}

#pragma mark 点击--分享
-(void)share{
    KKShareDialog *share=[[[NSBundle mainBundle] loadNibNamed:@"KKShareDialog" owner:self options:nil] lastObject];
    share.shareContent=@"xxxxxxx";
    share.shareCallbackBlock=^(int flag){
        if(flag==0){
            [KKAppUtils toast:@"分享失败"];
        }else{
            
        }
    };
    [share show];
}
#pragma mark 点击--向他咨询
- (IBAction)adviceClick:(id)sender {
    [KKMyHttp userCheck:^(id data) {
        KKAdviceChatVC *advice=[[KKAdviceChatVC alloc] init];
        advice.flag=0;
        advice.consultId=@"";
        advice.expertId=self.expertId;
        advice.expertName=self.expertInfo.realname;
        advice.expertCompany=self.expertInfo.company;
        [self.navigationController pushViewController:advice animated:YES];
    } failure:^(NSError *error) {
        
    }];
    
}

#pragma mark 接口--专家详情
-(void)getExpertDetail{
    [self showDialog];
    [KKComHttp getExpertDetail:self.expertId success:^(id data) {
        self.expertInfo=data;
        [self initData];
        [self dismissDialog];
    } failure:^(NSError *error) {
        [self dismissDialog];
    }];
}
#pragma mark 按钮--关注
- (IBAction)gZClick:(id)sender {
    if(self.gZBtn.isSelected){
        [self cancelFollow];
    }else{
        [self addFollow];
    }
    self.gZBtn.selected=!self.gZBtn.isSelected;
}

#pragma mark 接口--增加关注
-(void)addFollow{
    [KKMyHttp addFollow:self.expertInfo.expertid success:^(id data) {
        [KKAppUtils toast:@"关注成功"];
    } failure:^(NSError *error) {
        
    }];
}
#pragma mark 接口--取消关注
-(void)cancelFollow{
    [KKMyHttp cancelFollow:self.expertInfo.expertid success:^(id data) {
        [KKAppUtils toast:@"取消关注成功"];
    } failure:^(NSError *error) {
        
    }];
}

@end
